﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
using System.IO;
using Common;

namespace Workflow.Project
{
    public partial class SeachSampleOut : System.Web.UI.Page
    {
        private PJSearchManager objPJSearch = new PJSearchManager();
        public List<PJSampleOut> resultList = new List<PJSampleOut>();
        public SearchChoose objSearchChoose = new SearchChoose();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                resultList = objPJSearch.GetSampleOut(txtSapNo.Text.ToString().Trim());
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert({0})</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void Export_Click(object sender, EventArgs e)
        {
            List<PJSampleOut> exportResult = new List<PJSampleOut>();
            try
            {
                exportResult = objPJSearch.GetSampleOut(txtSapNo.Text.ToString().Trim());
                GridView GridView1 = new GridView();
                GridView1.AutoGenerateColumns = false;
                GridView1.Columns.Add(new BoundField() { DataField = "SerialNum", HeaderText = "样品订单流水号" });
                GridView1.Columns.Add(new BoundField() { DataField = "OrderType", HeaderText = "订单类型" });
                GridView1.Columns.Add(new BoundField() { DataField = "OrderStartSerialNum", HeaderText = "样品前置流水号" });
                GridView1.Columns.Add(new BoundField() { DataField = "Customer", HeaderText = "客户" });
                GridView1.Columns.Add(new BoundField() { DataField = "SaleOrder", HeaderText = "销售订单" });
                GridView1.Columns.Add(new BoundField() { DataField = "Bill", HeaderText = "交货单" });
                GridView1.Columns.Add(new BoundField() { DataField = "Bill2", HeaderText = "物流凭证" });
                GridView1.Columns.Add(new BoundField() { DataField = "Row", HeaderText = "行号" });
                GridView1.Columns.Add(new BoundField() { DataField = "CustomerOrder", HeaderText = "客户订单号" });
                GridView1.Columns.Add(new BoundField() { DataField = "Maktx", HeaderText = "产品编号" });
                GridView1.Columns.Add(new BoundField() { DataField = "Rev", HeaderText = "Sap号" });
                GridView1.Columns.Add(new BoundField() { DataField = "Description", HeaderText = "描述" });
                GridView1.Columns.Add(new BoundField() { DataField = "Num", HeaderText = "交货数量" });
                GridView1.Columns.Add(new BoundField() { DataField = "Ps", HeaderText = "备注" });
                GridView1.DataSource = exportResult;
                GridView1.DataBind();
                ExportToExcel(GridView1);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
        public void ExportToExcel(GridView GridView)
        {
            string excelname = DateTime.Now.ToString("yyyyMMddhhmm");
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=  " + excelname + " .xls");
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/type.xls";//设置输出文件类型为excel文件      
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }

        protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            string msgForm = objSearchChoose.Choose(ddlSelect);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", msgForm);
        }
    }
}